package com.example.demo.utils;

import org.apache.tomcat.util.bcel.classfile.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

/**
 * <p>Title: FileUtil.java</p>  
 * <p>Description: 文件工具类</p>  
 * @author tymac
 * @date 2017年12月22日
 */
public class FileUtil {
	
	private final static Logger log = LoggerFactory.getLogger(FileUtil.class);
	
	static private String path;
	public static String getpath() {
		if (path == null) {
			readProperty();
		}
		return path;
	}
	public static void setpath(String path) {
		FileUtil.path = path;
	}
	synchronized static public void readProperty() {
		if (path == null) {
			InputStream is = Constant.class.getResourceAsStream("/adapter.properties");
			Properties dbProps = new Properties();
			try {
				dbProps.load(is);
				path = dbProps.getProperty("file_path");
			} catch (Exception e) {
				System.err.println("不能读取属性文件. ");
			}
		}
	}

	public static String saveXmlFiles(String xmlStr, String interfacePath, String fileName) {
		String codeType = "UTF-8";
		String filePath = getpath() + interfacePath + "/" + DateUtil.getDateyyyyMMdd() + "/";
		File fw = new File(filePath);
		if (!fw.exists()) {
			fw.mkdirs();
		}
		filePath += fileName + ".xml";
		fw = new File(filePath);
		FileOutputStream fos=null;
		OutputStreamWriter osw=null;
		BufferedWriter bw=null;
		try {
			fos = new FileOutputStream(fw);
			osw = new OutputStreamWriter(fos, codeType);
			bw = new BufferedWriter(osw);
			bw.write(xmlStr);
		} catch (FileNotFoundException e) {
			log.error("**********保存报文异常-"+filePath+"-FileNotFoundException",e);
		} catch (UnsupportedEncodingException e) {
			log.error("**********保存报文异常-"+filePath+"-UnsupportedEncodingException",e);
		} catch (IOException e) {
			log.error("**********保存报文异常-"+filePath+"-IOException",e);
		}finally{
			try {
				bw.close();
				osw.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return filePath;
	}

	public static String saveJsonFiles(String jsonStr, String interfacePath, String fileName) {
		String codeType = "UTF-8";
		String filePath = getpath() + interfacePath + "/" + DateUtil.getDateyyyyMMdd() + "/";
		File fw = new File(filePath);
		if (!fw.exists()) {
			fw.mkdirs();
		}
		filePath += fileName + ".json";
		fw = new File(filePath);
		FileOutputStream fos=null;
		OutputStreamWriter osw=null;
		BufferedWriter bw=null;
		try {
			fos = new FileOutputStream(fw);
			osw = new OutputStreamWriter(fos, codeType);
			bw = new BufferedWriter(osw);
			bw.write(jsonStr);
		} catch (FileNotFoundException e) {
			log.error("**********保存报文异常-"+filePath+"-FileNotFoundException",e);
		} catch (UnsupportedEncodingException e) {
			log.error("**********保存报文异常-"+filePath+"-UnsupportedEncodingException",e);
		} catch (IOException e) {
			log.error("**********保存报文异常-"+filePath+"-IOException",e);
		}finally{
			try {
				bw.close();
				osw.close();
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return filePath;
	}
}
